var e = require("../../../../../../@babel/runtime/helpers/slicedToArray"), t = require("../../../../../../@babel/runtime/helpers/toConsumableArray"), a = require("../../../../../../common/vendor.js"), i = require("../z-paging-utils.js"), o = require("../z-paging-constant.js"), s = require("../z-paging-enum.js"), n = require("../z-paging-interceptor.js"), r = {
    props: {
        defaultPageNo: {
            type: [ Number, String ],
            default: i.u.gc("defaultPageNo", 1),
            observer: function(e) {
                this.pageNo = e;
            }
        },
        defaultPageSize: {
            type: [ Number, String ],
            default: i.u.gc("defaultPageSize", 10),
            validator: function(e) {
                return e <= 0 && i.u.consoleErr("default-page-size必须大于0！"), e > 0;
            }
        },
        dataKey: {
            type: [ Number, String, Object ],
            default: function() {
                return i.u.gc("dataKey", null);
            }
        },
        useCache: {
            type: Boolean,
            default: i.u.gc("useCache", !1)
        },
        cacheKey: {
            type: String,
            default: i.u.gc("cacheKey", null)
        },
        cacheMode: {
            type: String,
            default: i.u.gc("cacheMode", s.Enum.CacheMode.Default)
        },
        autowireListName: {
            type: String,
            default: i.u.gc("autowireListName", "")
        },
        autowireQueryName: {
            type: String,
            default: i.u.gc("autowireQueryName", "")
        },
        auto: {
            type: Boolean,
            default: i.u.gc("auto", !0)
        },
        reloadWhenRefresh: {
            type: Boolean,
            default: i.u.gc("reloadWhenRefresh", !0)
        },
        autoScrollToTopWhenReload: {
            type: Boolean,
            default: i.u.gc("autoScrollToTopWhenReload", !0)
        },
        autoCleanListWhenReload: {
            type: Boolean,
            default: i.u.gc("autoCleanListWhenReload", !0)
        },
        showRefresherWhenReload: {
            type: Boolean,
            default: i.u.gc("showRefresherWhenReload", !1)
        },
        showLoadingMoreWhenReload: {
            type: Boolean,
            default: i.u.gc("showLoadingMoreWhenReload", !1)
        },
        createdReload: {
            type: Boolean,
            default: i.u.gc("createdReload", !1)
        },
        localPagingLoadingTime: {
            type: [ Number, String ],
            default: i.u.gc("localPagingLoadingTime", 200)
        },
        useChatRecordMode: {
            type: Boolean,
            default: i.u.gc("useChatRecordMode", !1)
        },
        autoHideKeyboardWhenChat: {
            type: Boolean,
            default: i.u.gc("autoHideKeyboardWhenChat", !0)
        },
        concat: {
            type: Boolean,
            default: i.u.gc("concat", !0)
        },
        callNetworkReject: {
            type: Boolean,
            default: i.u.gc("callNetworkReject", !0)
        },
        value: {
            type: Array,
            default: function() {
                return [];
            }
        },
        modelValue: {
            type: Array,
            default: function() {
                return [];
            }
        }
    },
    data: function() {
        return {
            currentData: [],
            totalData: [],
            realTotalData: [],
            totalLocalPagingList: [],
            dataPromiseResultMap: {
                reload: null,
                complete: null,
                localPaging: null
            },
            isSettingCacheList: !1,
            pageNo: 1,
            currentRefreshPageSize: 0,
            isLocalPaging: !1,
            isAddedData: !1,
            isTotalChangeFromAddData: !1,
            privateConcat: !0,
            myParentQuery: -1,
            firstPageLoaded: !1,
            pagingLoaded: !1,
            loaded: !1,
            isUserReload: !0,
            fromEmptyViewReload: !1,
            queryFrom: "",
            listRendering: !1,
            isHandlingRefreshToPage: !1
        };
    },
    computed: {
        pageSize: function() {
            return this.defaultPageSize;
        },
        finalConcat: function() {
            return this.concat && this.privateConcat;
        },
        finalUseCache: function() {
            return this.useCache && !this.cacheKey && i.u.consoleErr("use-cache为true时，必须设置cache-key，否则缓存无效！"), 
            this.useCache && !!this.cacheKey;
        },
        finalCacheKey: function() {
            return this.cacheKey ? "".concat(o.c.cachePrefixKey, "-").concat(this.cacheKey) : null;
        },
        isFirstPage: function() {
            return this.pageNo === this.defaultPageNo;
        }
    },
    watch: {
        totalData: function(e, t) {
            this._totalDataChange(e, t);
        },
        currentData: function(e, t) {
            this._currentDataChange(e, t);
        },
        useChatRecordMode: function(e, t) {
            e && (this.nLoadingMoreFixedHeight = !1);
        },
        value: {
            handler: function(e) {
                this.realTotalData = e;
            },
            immediate: !0
        },
        modelValue: {
            handler: function(e) {
                this.realTotalData = e;
            },
            immediate: !0
        }
    },
    methods: {
        complete: function(e) {
            var t = !(arguments.length > 1 && void 0 !== arguments[1]) || arguments[1];
            return this.customNoMore = -1, this.addData(e, t);
        },
        completeByKey: function(e) {
            var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : null, a = !(arguments.length > 2 && void 0 !== arguments[2]) || arguments[2];
            return null !== t && null !== this.dataKey && t !== this.dataKey ? (this.isFirstPage && this.endRefresh(), 
            new Promise(function(e) {
                return e();
            })) : (this.customNoMore = -1, this.addData(e, a));
        },
        completeByTotal: function(e, t) {
            var a = this, i = !(arguments.length > 2 && void 0 !== arguments[2]) || arguments[2];
            if ("undefined" == t) this.customNoMore = -1; else {
                var o = this._checkDataType(e, i, !1);
                if (e = o.data, i = o.success, t >= 0 && i) return new Promise(function(o, s) {
                    a.$nextTick(function() {
                        var n = !1, r = (a.pageNo == a.defaultPageNo ? 0 : a.realTotalData.length) + (a.privateConcat ? e.length : 0) - t;
                        r >= 0 && (n = !0, r = a.defaultPageSize - r, a.privateConcat && r > 0 && r < e.length && (e = e.splice(0, r))), 
                        a.completeByNoMore(e, n, i).then(function(e) {
                            return o(e);
                        }).catch(function() {
                            return s();
                        });
                    });
                });
            }
            return this.addData(e, i);
        },
        completeByNoMore: function(e, t) {
            var a = !(arguments.length > 2 && void 0 !== arguments[2]) || arguments[2];
            return "undefined" != t && (this.customNoMore = 1 == t ? 1 : 0), this.addData(e, a);
        },
        completeByError: function(e) {
            return this.customerEmptyViewErrorText = e, this.complete(!1);
        },
        addData: function(e) {
            var t = this, a = !(arguments.length > 1 && void 0 !== arguments[1]) || arguments[1];
            this.fromCompleteEmit || (this.disabledCompleteEmit = !0, this.fromCompleteEmit = !1);
            var o = i.u.getTime() - this.requestTimeStamp, s = this.minDelay;
            this.isFirstPage && this.finalShowRefresherWhenReload && (s = Math.max(400, s));
            var n = this.requestTimeStamp > 0 && o < s ? s - o : 0;
            return this.$nextTick(function() {
                i.u.delay(function() {
                    t._addData(e, a, !1);
                }, t.delay > 0 ? t.delay : n);
            }), new Promise(function(e, a) {
                t.dataPromiseResultMap.complete = {
                    resolve: e,
                    reject: a
                };
            });
        },
        addDataFromTop: function(e) {
            var a = this, o = !(arguments.length > 1 && void 0 !== arguments[1]) || arguments[1], s = !(arguments.length > 2 && void 0 !== arguments[2]) || arguments[2];
            e = "[object Array]" !== Object.prototype.toString.call(e) ? [ e ] : e.reverse(), 
            this.finalUseVirtualList && this._setCellIndex(e, "top"), this.totalData = [].concat(t(e), t(this.totalData)), 
            o && i.u.delay(function() {
                return a._scrollToTop(s);
            });
        },
        resetTotalData: function(e) {
            this.isTotalChangeFromAddData = !0, e = "[object Array]" !== Object.prototype.toString.call(e) ? [ e ] : e, 
            this.totalData = e;
        },
        addChatRecordData: function(e) {
            var a = this, o = !(arguments.length > 1 && void 0 !== arguments[1]) || arguments[1], s = !(arguments.length > 2 && void 0 !== arguments[2]) || arguments[2];
            e = "[object Array]" !== Object.prototype.toString.call(e) ? [ e ] : e, this.useChatRecordMode && (this.isTotalChangeFromAddData = !0, 
            this.totalData = [].concat(t(this.totalData), t(e)), o && i.u.delay(function() {
                a._scrollToBottom(s);
            }));
        },
        setLocalPaging: function(e) {
            var t = this, a = !(arguments.length > 1 && void 0 !== arguments[1]) || arguments[1];
            return this.isLocalPaging = !0, this.$nextTick(function() {
                t._addData(e, a, !0);
            }), new Promise(function(e, a) {
                t.dataPromiseResultMap.localPaging = {
                    resolve: e,
                    reject: a
                };
            });
        },
        reload: function() {
            var e = this, t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : this.showRefresherWhenReload;
            return t && (this.privateShowRefresherWhenReload = t, this.isUserPullDown = !0), 
            this.showLoadingMoreWhenReload || (this.listRendering = !0), this.$nextTick(function() {
                e._preReload(t, !1);
            }), new Promise(function(t, a) {
                e.dataPromiseResultMap.reload = {
                    resolve: t,
                    reject: a
                };
            });
        },
        refresh: function() {
            return this._handleRefreshWithDisPageNo(this.pageNo - this.defaultPageNo + 1);
        },
        refreshToPage: function(e) {
            return this.isHandlingRefreshToPage = !0, this._handleRefreshWithDisPageNo(e + this.defaultPageNo - 1);
        },
        updateCache: function() {
            this.finalUseCache && this.totalData.length && this._saveLocalCache(this.totalData.slice(0, Math.min(this.totalData.length, this.pageSize)));
        },
        clean: function() {
            this._reload(!0), this._addData([], !0, !1);
        },
        clear: function() {
            this.clean();
        },
        doChatRecordLoadMore: function() {
            this.useChatRecordMode && this._onLoadingMore("click");
        },
        _preReload: function() {
            var e = this, t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : this.showRefresherWhenReload, a = !(arguments.length > 1 && void 0 !== arguments[1]) || arguments[1], n = this.finalRefresherEnabled && this.useCustomRefresher;
            -1 === this.customRefresherHeight && n ? i.u.delay(function() {
                return e._preReload(t, a);
            }, o.c.delayTime / 2) : (this.isUserReload = !0, this.loadingType = s.Enum.LoadingType.Refresher, 
            t ? (this.privateShowRefresherWhenReload = t, this.useCustomRefresher ? this._doRefresherRefreshAnimate() : this.refresherTriggered = !0) : this._refresherEnd(!1, !1, !1, !1), 
            this._reload(!1, a));
        },
        _reload: function() {
            var e = arguments.length > 0 && void 0 !== arguments[0] && arguments[0], t = arguments.length > 1 && void 0 !== arguments[1] && arguments[1], a = arguments.length > 2 && void 0 !== arguments[2] && arguments[2];
            if (this.isAddedData = !1, this.insideOfPaging = -1, this.cacheScrollNodeHeight = -1, 
            this.pageNo = this.defaultPageNo, this._cleanRefresherEndTimeout(), !this.privateShowRefresherWhenReload && !e && this._startLoading(!0), 
            this.firstPageLoaded = !0, this.isTotalChangeFromAddData = !1, this.isSettingCacheList || (this.totalData = []), 
            !e) {
                this._emitQuery(this.pageNo, this.defaultPageSize, a ? s.Enum.QueryFrom.UserPullDown : s.Enum.QueryFrom.Reload);
                var o = 0;
                i.u.delay(this._callMyParentQuery, o), !t && this.autoScrollToTopWhenReload && this._scrollToTop(!1);
            }
        },
        _addData: function(e, t, a) {
            var n = this;
            this.isAddedData = !0, this.fromEmptyViewReload = !1, this.isTotalChangeFromAddData = !0, 
            this.refresherTriggered = !1, this._endSystemLoadingAndRefresh();
            var r = this.isUserPullDown;
            this.showRefresherUpdateTime && this.isFirstPage && (i.u.setRefesrherTime(i.u.getTime(), this.refresherUpdateTimeKey), 
            this.$refs.refresh && this.$refs.refresh.updateTime()), !a && r && this.isFirstPage && (this.isUserPullDown = !1), 
            this.isFirstPage ? this.listRendering = !1 : (this.listRendering = !0, this.$nextTick(function() {
                i.u.delay(function() {
                    return n.listRendering = !1;
                });
            }));
            var h = this._checkDataType(e, t, a);
            e = h.data, t = h.success;
            var l = o.c.delayTime;
            if (this.loadingForNow = !1, i.u.delay(function() {
                n.pagingLoaded = !0, n.$nextTick(function() {
                    !a && n._refresherEnd(l > 0, !0, r);
                });
            }), this.isFirstPage && (this.isLoadFailed = !t, this.$emit("isLoadFailedChange", this.isLoadFailed), 
            this.finalUseCache && t && (this.cacheMode === s.Enum.CacheMode.Always || this.isSettingCacheList) && this._saveLocalCache(e)), 
            this.isSettingCacheList = !1, t) {
                if ((!1 !== this.privateConcat || this.isHandlingRefreshToPage || this.loadingStatus !== s.Enum.More.NoMore) && (this.loadingStatus = s.Enum.More.Default), 
                a) {
                    this.totalLocalPagingList = e;
                    var u = this.defaultPageNo, c = this.queryFrom !== s.Enum.QueryFrom.Refresh ? this.defaultPageSize : this.currentRefreshPageSize;
                    this._localPagingQueryList(u, c, 0, function(e) {
                        n.completeByTotal(e, n.totalLocalPagingList.length);
                    });
                } else {
                    i.u.delay(function() {
                        n._currentDataChange(e, n.currentData), n._callDataPromise(!0, n.totalData);
                    }, 0);
                }
                this.isHandlingRefreshToPage && (this.isHandlingRefreshToPage = !1, this.pageNo = this.defaultPageNo + Math.ceil(e.length / this.pageSize) - 1, 
                e.length % this.pageSize != 0 && (this.customNoMore = 1));
            } else this._currentDataChange(e, this.currentData), this._callDataPromise(!1), 
            this.loadingStatus = s.Enum.More.Fail, this.isHandlingRefreshToPage = !1, this.loadingType === s.Enum.LoadingType.LoadingMore && this.pageNo--;
        },
        _totalDataChange: function(e, t) {
            var a = this, s = !(arguments.length > 2 && void 0 !== arguments[2]) || arguments[2];
            (this.isUserReload && this.autoCleanListWhenReload || !this.firstPageLoaded || e.length || !t.length) && (this._doCheckScrollViewShouldFullHeight(e), 
            this.realTotalData.length || e.length || (s = !1), this.realTotalData = e, s && (this.$emit("input", e), 
            this.$emit("update:modelValue", e), this.$emit("update:list", e), this.$emit("listChange", e), 
            this._callMyParentList(e)), this.firstPageLoaded = !1, this.isTotalChangeFromAddData = !1, 
            this.$nextTick(function() {
                i.u.delay(function() {
                    a._getNodeClientRect(".zp-paging-container-content").then(function(e) {
                        e && a.$emit("contentHeightChanged", e[0].height);
                    });
                }, o.c.delayTime * (a.isIos ? 1 : 3));
            }));
        },
        _currentDataChange: function(e, a) {
            var o = this;
            if (e = t(e), this.finalUseVirtualList && this._setCellIndex(e, "bottom"), this.useChatRecordMode && e.reverse(), 
            this.isFirstPage && this.finalConcat && (this.totalData = []), -1 !== this.customNoMore ? (1 === this.customNoMore || 0 !== this.customNoMore && !e.length) && (this.loadingStatus = s.Enum.More.NoMore) : (!e.length || e.length && e.length < this.defaultPageSize) && (this.loadingStatus = s.Enum.More.NoMore), 
            this.totalData.length) if (this.useChatRecordMode) {
                var n = e.length, r = "z-paging-".concat(n);
                this.totalData = [].concat(t(e), t(this.totalData)), this.pageNo !== this.defaultPageNo ? (this.privateScrollWithAnimation = 0, 
                this.$emit("update:chatIndex", n), this.$nextTick(function() {
                    o._scrollIntoView(r, 30 + Math.max(0, o.cacheTopHeight), !1, function() {
                        o.$emit("update:chatIndex", 0);
                    });
                })) : this.$nextTick(function() {
                    o._scrollToBottom(!1);
                });
            } else if (this.finalConcat) {
                var h = this.oldScrollTop;
                this.totalData = [].concat(t(this.totalData), t(e)), this.isIos || this.refresherOnly || this.usePageScroll || !e.length || (this.loadingMoreTimeStamp = i.u.getTime(), 
                this.$nextTick(function() {
                    o.scrollToY(h);
                }));
            } else this.totalData = e; else this.finalConcat, this.totalData = e, this.useChatRecordMode && this.$nextTick(function() {
                o._scrollToBottom(!1);
            });
            this.privateConcat = !0;
        },
        _handleRefreshWithDisPageNo: function(e) {
            var t = this;
            if (!this.isHandlingRefreshToPage && !this.realTotalData.length) return this.reload();
            if (e >= 1) {
                this.loading = !0, this.privateConcat = !1;
                var a = e * this.pageSize;
                this.currentRefreshPageSize = a, this.isLocalPaging && this.isHandlingRefreshToPage ? this._localPagingQueryList(this.defaultPageNo, a, 0, function(e) {
                    t.complete(e);
                }) : (this._emitQuery(this.defaultPageNo, a, s.Enum.QueryFrom.Refresh), this._callMyParentQuery(this.defaultPageNo, a));
            }
            return new Promise(function(e, a) {
                t.dataPromiseResultMap.reload = {
                    resolve: e,
                    reject: a
                };
            });
        },
        _localPagingQueryList: function(e, a, o, s) {
            e = Math.max(1, e), a = Math.max(1, a);
            var n = t(this.totalLocalPagingList), r = (e - 1) * a, h = Math.min(n.length, r + a), l = n.splice(r, h - r);
            i.u.delay(function() {
                return s(l);
            }, o);
        },
        _saveLocalCache: function(e) {
            a.index.setStorageSync(this.finalCacheKey, e);
        },
        _setListByLocalCache: function() {
            this.totalData = a.index.getStorageSync(this.finalCacheKey) || [], this.isSettingCacheList = !0;
        },
        _callMyParentList: function(e) {
            if (this.autowireListName.length) {
                var t = i.u.getParent(this.$parent);
                t && t[this.autowireListName] && (t[this.autowireListName] = e);
            }
        },
        _callMyParentQuery: function() {
            var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : 0, t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 0;
            if (this.autowireQueryName) {
                if (-1 === this.myParentQuery) {
                    var a = i.u.getParent(this.$parent);
                    a && a[this.autowireQueryName] && (this.myParentQuery = a[this.autowireQueryName]);
                }
                -1 !== this.myParentQuery && (t > 0 ? this.myParentQuery(e, t) : this.myParentQuery(this.pageNo, this.defaultPageSize));
            }
        },
        _emitQuery: function(a, o, s) {
            this.queryFrom = s, this.requestTimeStamp = i.u.getTime();
            var r = this.realTotalData.slice(-1), h = e(r, 1)[0];
            this.$emit.apply(this, [ "query" ].concat(t(n.interceptor._handleQuery(a, o, s, h || null))));
        },
        _callDataPromise: function(e, t) {
            for (var a in this.dataPromiseResultMap) {
                var i = this.dataPromiseResultMap[a];
                i && (e ? i.resolve({
                    totalList: t,
                    noMore: this.loadingStatus === s.Enum.More.NoMore
                }) : this.callNetworkReject && i.reject("z-paging-".concat(a, "-error")));
            }
        },
        _checkDataType: function(e, t, a) {
            var o = Object.prototype.toString.call(e);
            return "[object Boolean]" === o ? (t = e, e = []) : "[object Array]" !== o && (e = [], 
            "[object Undefined]" !== o && "[object Null]" !== o && i.u.consoleErr((a ? "setLocalPaging" : "complete") + "参数类型不正确，第一个参数类型必须为Array!")), 
            {
                data: e,
                success: t
            };
        }
    }
};

exports.dataHandleModule = r;